/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package dao;


import java.util.List;
import org.hibernate.Transaction;
import org.hibernate.Query;
import org.hibernate.Session;
import pojo.Sanpham;
import pojo.Sanphamyeuthich;
import pojo.Taikhoan;
import util.HibernateUtil;

/**
 *
 * @author PHT
 */
public class SanPhamYeuThichDAO {
    
    public static int pageCount;
    public static int listCount; // Số lượng của danh sách
    
    public static int tinhTrangTheoDSSP(int soLuong, int soLuongSanPhamTrenTrang) {
        int soTrang = 0;

        if (soLuongSanPhamTrenTrang == -1) {
            soTrang = 1;
        } else {
            soTrang = soLuong / soLuongSanPhamTrenTrang;
            if (soLuong % soLuongSanPhamTrenTrang != 0) {
                soTrang++;
            }
        }
        return soTrang;
    }
    
    public static List<Sanpham> LaySanPhamYeuThich(Taikhoan tk, int page)
    {
        Session session = HibernateUtil.getSessionFactory().openSession();
        List<Sanpham> listSP = null;
        try {
            session.beginTransaction();
            String hql = "select distinct sp.sanpham from Sanphamyeuthich sp where sp.taikhoan=:tk and sp.daHuy=:daHuy";
            Query query = session.createQuery(hql);
            query.setParameter("tk", tk);
            query.setBoolean("daHuy", false);
            
            int numberProducts = 8;
            // Tính số trang :
            listSP = query.list();
            pageCount = tinhTrangTheoDSSP(listSP.size(), numberProducts);
            listCount = listSP.size();

            int n = (page - 1) * numberProducts;

            if (numberProducts != -1) {
                query.setFirstResult(n);
                query.setMaxResults(numberProducts);
            }
            listSP = query.list();
            
        } catch (Exception ex) {
            listSP = null;
        } finally {
            session.close();
        }
        return listSP;
    }
    
    public static List<Sanpham> TimKiemSanPhamYeuThich(String key, Taikhoan tk, int page)
    {
        Session session = HibernateUtil.getSessionFactory().openSession();
        List<Sanpham> listSP = null;
        try {
            session.beginTransaction();
            String hql = "select distinct sp.sanpham from Sanphamyeuthich sp where sp.taikhoan=:tk and sp.daHuy=:daHuy ";
            hql += "and sp.sanpham.tenSanPham like CONCAT ('%', :tenSP ,'%') ";
            Query query = session.createQuery(hql);
            query.setParameter("tk", tk);
            query.setString("tenSP", key);
            query.setBoolean("daHuy", false);
            
            int numberProducts = 8;
            // Tính số trang :
            listSP = query.list();
            pageCount = tinhTrangTheoDSSP(listSP.size(), numberProducts);
            listCount = listSP.size();

            int n = (page - 1) * numberProducts;

            if (numberProducts != -1) {
                query.setFirstResult(n);
                query.setMaxResults(numberProducts);
            }
            listSP = query.list();
            
        } catch (Exception ex) {
            listSP = null;
        } finally {
            session.close();
        }
        return listSP;
    }
    
    
    public static boolean CapNhatKhongThichSanPham(Taikhoan tk, int maSP)
    {
        boolean result = false;
        Session session = HibernateUtil.getSessionFactory().openSession();
        Transaction transaction = null;
        try
        {
            transaction = session.beginTransaction();
            String hql = "select distinct sp from Sanphamyeuthich sp where sp.taikhoan=:tk and sp.sanpham.maSanPham=:maSP ";
            Query query = session.createQuery(hql);
            query.setParameter("tk", tk);
            query.setInteger("maSP", maSP);
            Sanphamyeuthich sanpham = (Sanphamyeuthich)query.uniqueResult();
            
            Sanphamyeuthich sp = (Sanphamyeuthich)session.get(Sanphamyeuthich.class, sanpham.getMaYeuThich());
            sp.setDaHuy(true);
            session.update(sp);
            transaction.commit();
            result = true;
        }
        catch (Exception ex) {
            session.getTransaction().rollback();
            System.out.println(ex.getMessage());
            result = false;
        }
        finally {
            session.close();
        }
        
        return result;
    }
    
}
